<?php
class Project extends CI_Controller
{
	private $data = array();

	function __construct(){
		parent::__construct();
		// autoload helper and library
		$this->layout->setLayout('admin');
		$this->load->model('project_model');
		$this->data['error'] = '';
		$auth = $this->session->userdata('auth');
		if(!$auth)
		{
			redirect('/auth/login');
		}
	}
	function index(){
		$config['base_url'] =base_url()."index.php/project/index";
		$config['total_rows'] = $this->db->count_all('project');
		$config['per_page'] = '10';
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		//----------------
		$offset = $this->uri->segment(3);
		$project = $this->project_model->getListProject($offset,$config['per_page']);
		$this->data['project'] = $project;
		$this->layout->view('listproject',$this->data);
	}
	function addProject(){
		$this->data['project']->id = '';
		//$this->project_model->getLastProjectId() + 1
		$this->data['project']->name = '';
		$this->data['action'] = "submitAddProject";
		$this->layout->view('addproject',$this->data);
	}
	function submitAddProject()
	{
		$projectname = $this->input->post('name');
		if (trim($projectname) == ""){
			$this->data['error'] = 'Xin đặt tên project';
			$this->addProject();
		}else{
			$project = array('name'=>$projectname);
			$this->project_model->addProject($project);
			redirect('project');
		}
	}
	function delete(){
		$projectId = $this->input->post('hidID');
		$arrProjectIdId = $this->input->post('chkid');

		if($projectId){
			$this->project_model->delete($projectId);
		}
		else{
			foreach ($arrProjectIdId as $id):
			$this->project_model->delete($id);
			endforeach;
		}
		redirect('project/index/');
	}
	function update()
	{
		$projectId = $this->uri->segment(3);
		$this->data['project'] = $this->project_model->getProject($projectId);
		$this->data['action'] = "submitUpdateProject";
		$this->layout->view('addproject',$this->data);
	}
	function submitUpdateProject()
	{
		$projectName = $this->input->post('name');
		$projectId = $this->input->post('id');
		if (trim($projectName) == ""){
			$this->data['error'] = 'Xin đặt tên project';
			$this->update();
		}else{
			$project = array('id'=>$projectId,'name'=>$projectName);
			$this->project_model->update($project);
			redirect('project');
		}
	}
}